package lp.polimi.treefinder;

import java.util.Locale;

import lp.polimi.treefinder.utils.FilesUtils;
import lp.polimi.treefinder.utils.UIUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.SimpleFacebook.OnLoginListener;
import com.sromku.simple.fb.SimpleFacebook.OnLogoutListener;
import com.sromku.simple.fb.SimpleFacebook.OnPublishListener;
import com.sromku.simple.fb.entities.Photo;

/**
 * This activity allows the user to share his tag on Facebook. "Simple Facebook" is used here, which is
 * a small API built on top of the Facebook API for Android, which is meant to ease some of the most
 * common sharing tasks (e.g. posting a comment or a photo) for which the official API is really
 * too complex (and badly documented also).
 */
public class FacebookShareActivity extends Activity {



	private SimpleFacebook mSimpleFacebook;

	private ProgressDialog mProgress;
	private Button mButtonShare;
	private Bitmap leafBitmap;
	private EditText multiLineEditText;


	private OnPublishListener onPublishListener = new OnPublishListener() {

		@Override
		public void onFail(String reason) {
			hideDialog();
			toast(getResources().getString(R.string.sharing_error));
			UIUtils.lockScreenRotation(FacebookShareActivity.this);
			Log.w("Facebook", "Failed to publish");
		}

		@Override
		public void onException(Throwable throwable) {
			hideDialog();
			toast(getResources().getString(R.string.sharing_error));
			UIUtils.unlockScreenRotation(FacebookShareActivity.this);
			Log.e("Facebook", "Bad thing happened", throwable);
		}

		@Override
		public void onThinking() {
			UIUtils.lockScreenRotation(FacebookShareActivity.this);
			showDialog();
		}

		@Override
		public void onComplete(String id) {
			toast(getResources().getString(R.string.sharing_success));
			mSimpleFacebook.logout(mOnLogoutListener);
		}
	};


	private OnLoginListener mOnLoginListener = new OnLoginListener() {

		@Override
		public void onFail(String reason) {
			Log.e("Facebook", "Failed to login");
			toast(getResources().getString(R.string.login_error));
			UIUtils.unlockScreenRotation(FacebookShareActivity.this);
		}

		@Override
		public void onException(Throwable throwable) {
			toast(getResources().getString(R.string.login_error));
			UIUtils.unlockScreenRotation(FacebookShareActivity.this);
			Log.e("Facebook", "Bad thing happened", throwable);
		}

		@Override
		public void onThinking() {
			UIUtils.lockScreenRotation(FacebookShareActivity.this);
		}

		@Override
		public void onLogin() {
			Log.i("Facebook", "Logged in");
			publishPhoto();
		}

		@Override
		public void onNotAcceptingPermissions() {
			toast(getResources().getString(R.string.facebook_read_permission_error));
			UIUtils.unlockScreenRotation(FacebookShareActivity.this);
		}
	};

	private OnLogoutListener mOnLogoutListener = new OnLogoutListener() {

		@Override
		public void onFail(String reason) {
			hideDialog();
			Log.w("Facebook", "Failed to logout");
			UIUtils.unlockScreenRotation(FacebookShareActivity.this);
		}

		@Override
		public void onException(Throwable throwable) {
			hideDialog();
			Log.e("Facebook", "Bad thing happened", throwable);
			UIUtils.unlockScreenRotation(FacebookShareActivity.this);
		}

		@Override
		public void onThinking() {
			// doNothing
		}

		@Override
		public void onLogout() {
			hideDialog();
			UIUtils.unlockScreenRotation(FacebookShareActivity.this);
			finish();
			Log.i("Facebook", "Logged out");
		}

	};


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		leafBitmap = FilesUtils.retrieveLeafBitmap(this, getIntent().getExtras().getString(XMLHandler.KEY_NAME), "jpg");


		setContentView(R.layout.activity_facebook_share);

		mButtonShare = (Button)findViewById(R.id.share_button);
		mButtonShare.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (!mSimpleFacebook.isLogin())
					mSimpleFacebook.login(mOnLoginListener);
				else
					publishPhoto();
			}
		});

		multiLineEditText = (EditText) findViewById(R.id.share_text);
		multiLineEditText.setText((getIntent().getExtras().getString(XMLHandler.KEY_NAME)).toUpperCase(Locale.US)
				+"\n"+getResources().getString(R.string.shared_message)
				+"\n"+getResources().getString(R.string.app_url));

		ImageView image = (ImageView)findViewById(R.id.share_leaf);
		image.setImageBitmap(leafBitmap);

	}


	@Override
	protected void onResume() {
		super.onResume();
		mSimpleFacebook = SimpleFacebook.getInstance(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data);
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void toast(String message) {
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
	}



	private void showDialog() {
		mProgress = ProgressDialog.show(this, getResources().getString(R.string.shareFB),
				getResources().getString(R.string.sharing_process), true);
	}

	private void hideDialog() {
		mProgress.hide();
	}

	private void publishPhoto() {
		Photo photo = new Photo(leafBitmap);
		photo.addDescription(multiLineEditText.getText().toString());
		mSimpleFacebook.publish(photo, onPublishListener);
	}

}